package com.paireach.szwolaiyun.newFragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.paireach.szwolaiyun.R;
import com.paireach.szwolaiyun.activity.Activity_A;
import com.paireach.szwolaiyun.activity.Activity_C;
import com.paireach.szwolaiyun.activity.Activity_D;
import com.paireach.szwolaiyun.sz_common_widget.adapter.AppBaseAdapter;
import com.paireach.szwolaiyun.sz_common_widget.adapter.ViewHolder;
import com.paireach.szwolaiyun.sz_common_widget.fragment.BaseFragment;
import com.paireach.szwolaiyun.sz_util.SharedPreferencesUtil;

import java.util.ArrayList;
import java.util.List;

import szwolaiyun.paireach.com.szwolaizhuang.model.business.DriveOrder;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.AppEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.HttpErrorEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.HttpSuccessEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.requestTag.RequestTag;
import szwolaiyun.paireach.com.szwolaizhuang.net.WayBillRequest;
import szwolaiyun.paireach.com.szwolaizhuang.net.json.JsonPaser;

/**
 * 多地装货fragment
 * Created by Administrator on 2016/6/23.
 */
public class RemoteLoadFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, AdapterView.OnItemClickListener {
    private static final String TAG = "RemoteLoadFragment";
    private View view;
    private String phoneNumber;
    private ListView listView;
    private ListAdapter adapter;
    private SwipeRefreshLayout refreshLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return view = inflater.inflate(R.layout.fragment_remote_load, container, false);
    }

    @Override
    public void httpErrorEvent(HttpErrorEvent event) {
        super.httpErrorEvent(event);
        if (event.getRequestTag() == RequestTag.GET_ALL_WAY_BILL) {
            dismissProgressDialog();
            showToast(event.getErrorMessage());
            if (refreshLayout.isRefreshing()) {
                refreshLayout.setRefreshing(false);
            }
        }
    }

    @Override
    public void httpSuccessEvent(HttpSuccessEvent event) {
        super.httpSuccessEvent(event);
        if (event.getRequestTag() == RequestTag.GET_ALL_WAY_BILL) {
            dismissProgressDialog();
            JsonPaser.parseAllOrder(event.getJson());
            if (refreshLayout.isRefreshing()) {
                refreshLayout.setRefreshing(false);
            }
            Log.d(TAG, "消息event.getJson()=" + event.getJson());
        }
    }

    @Override
    public void applicationEvent(AppEvent event) {
        super.applicationEvent(event);
        if (event.getTag().equals("all_order_info")) {
            if (event.getCode() == 0) {
                List<DriveOrder> list = (List<DriveOrder>) event.getObj1();
//                for (int i = 0; i < list.size(); i++) {
//                    if (list.get(i).getOrderState().equals("装货在途"))
//                        list.remove(i);
//                }
                adapter.setListAndRefresh(list);
                //运单中心不显示直发已签收
//                setListAndRefresh2(list);
            } else {
                ArrayList<DriveOrder> arrayList = new ArrayList<>();
                adapter.setListAndRefresh(arrayList);//直接调用notifyDataSetChanged无效
                showToast(event.getExtraInfo());
            }
            if (refreshLayout.isRefreshing()) {
                refreshLayout.setRefreshing(false);
            }
        }
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        phoneNumber = SharedPreferencesUtil.getValue(getActivity(), SharedPreferencesUtil.USER_PHONE);
        adapter = new ListAdapter(getActivity());
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    @Override
    public void onResume() {
        super.onResume();
        showProgressDialog();
        new WayBillRequest().loadAllOrder(phoneNumber);
        Log.d(TAG, "RemoteLoadFragment---onResume");
    }

    private void initView() {
        listView = (ListView) view.findViewById(R.id.dispatchList);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
        refreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refreshLayout);
        refreshLayout.setColorSchemeResources(R.color.item3, R.color.item11, R.color.gold);
        refreshLayout.setOnRefreshListener(this);
        view.findViewById(R.id.layout_top_back).setVisibility(View.GONE);
        ((TextView) view.findViewById(R.id.layout_top_change)).setText("运单中心");
    }

    @Override
    public void onRefresh() {
        new WayBillRequest().loadAllOrder(phoneNumber);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        DriveOrder driveOrder = (DriveOrder) adapter.getItem(position);
        Bundle bundle = new Bundle();
        bundle.putSerializable("DriveOrder", driveOrder);
//        int stateCode = driveOrder.getOrderStateCode();
//        if (stateCode <= 220) {//司机未接运单前的状态
//            //屬於界面A的，詳細看設計圖
//            Intent intentA = new Intent(getActivity(), Activity_A.class);
//            intentA.putExtras(bundle);
//            startActivity(intentA);
//        } else if (stateCode == 224 || stateCode == 226 || stateCode == 236 || stateCode == 238) {
//            //屬於界面D的，詳細看設計圖   判断能否进场
//            Intent intentD = new Intent(getActivity(), Activity_D.class);
//            intentD.putExtras(bundle);
//            startActivity(intentD);
//        } else {
//            //屬於界面C的，詳細看設計圖
//            Intent intentC = new Intent(getActivity(), Activity_C.class);
//            intentC.putExtras(bundle);
//            startActivity(intentC);
//        }

        //调转到A界面，对A进行处理
        Intent intentA = new Intent(getActivity(), Activity_A.class);
        intentA.putExtras(bundle);
        startActivity(intentA);
    }

    private class ListAdapter extends AppBaseAdapter {

        public ListAdapter(Context mContext) {
            super(mContext);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = ViewHolder.get(getContext(), convertView, parent, R.layout.item_order_in_way2, position);
            DriveOrder order = (DriveOrder) getItem(position);
            ((TextView) holder.getView(R.id.tv_home_ordercode)).setText(order.getOrderNo());
            ((TextView) holder.getView(R.id.tv_ordertime)).setText(order.getPlaceTime());
            ((TextView) holder.getView(R.id.tv_originname)).setText(order.getCustomerName());
//            ((TextView) holder.getView(R.id.tv_origin_address)).setText(order.getStartPlaceDetailAddress());
            ((TextView) holder.getView(R.id.tv_endname)).setText(order.getSupplierName());
//            ((TextView) holder.getView(R.id.tv_end_address)).setText(order.getEndPlaceDetailAddress());
            TextView txt_state = holder.getView(R.id.txt_state);
            txt_state.setVisibility(View.VISIBLE);
//            if (order.getAcceptByDriverTime() != null) {
//                txt_state.setText("已接:" + order.getOrderState());//显示 运单状态
//                txt_state.setTextColor(Color.WHITE);
//                txt_state.setBackgroundResource(R.color.item12);
//            } else {
//                txt_state.setText("未接");
//                txt_state.setTextColor(Color.BLACK);
//                txt_state.setBackgroundResource(R.color.ivory);
//            }
            txt_state.setText("" + order.getOrderState());//显示 运单状态
            return holder.getConvertView();
        }
    }

//    //运单中心中去除直达已签收的运单2016-9-2
//    public void setListAndRefresh2(List<DriveOrder> tmpList) {
////        clearList();
//        List<DriveOrder> driveOrders = new ArrayList<>();
//        for (int i = 0; i < tmpList.size(); i++) {
//            DriveOrder orderInfo = tmpList.get(i);
//            if (!(orderInfo.getAlloStraType().equals("直发") && (orderInfo.getOrderStateCode() == 246 || orderInfo.getOrderStateCode() == 247))) {
//                driveOrders.add(orderInfo);
//            }
//        }
//        //去除已签收直发的在刷新显示
//        adapter.setListAndRefresh(driveOrders);
//    }
}
